package top.gangster.lunan.contractservice.service.impl.vo;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.gangster.lunan.contractservice.entity.Client;
import top.gangster.lunan.contractservice.entity.Contract;
import top.gangster.lunan.contractservice.entity.ContractField;
import top.gangster.lunan.contractservice.entity.ContractState;
import top.gangster.lunan.contractservice.entity.ContractType;
import top.gangster.lunan.contractservice.entity.vo.ContractVo;
import top.gangster.lunan.contractservice.service.ClientService;
import top.gangster.lunan.contractservice.service.ContractFieldService;
import top.gangster.lunan.contractservice.service.ContractService;
import top.gangster.lunan.contractservice.service.ContractStateService;
import top.gangster.lunan.contractservice.service.ContractTypeService;
import top.gangster.lunan.contractservice.service.vo.ContractVoService;
import top.gangster.lunan.util.WrapperUtils;

@Service
public class ContractVoServiceImpl implements ContractVoService {
	@Autowired
	ContractService contractService;
	@Autowired
	ContractFieldService contractFieldService;
	@Autowired
	ClientService clientService;
	@Autowired
	ContractStateService contractStateService;
	@Autowired
	ContractTypeService contractTypeService;


	/**
	 * 根据ContractId获取ContractVo
	 * @param contractId 合同id
	 * @return contractVo
	 */
	@Override
	public ContractVo getVoByContractId(String contractId) {
		Contract contract = contractService.getById(contractId);
		return this.getVoByContract(contract);
	}

	/**
	 * 根据ContractIdList获取ContractVoList
	 * @param contractIdList 合同id集合
	 * @return contractVoList
	 */
	@Override
	public List<ContractVo> getVoByContractIdList(List<String> contractIdList) {
		QueryWrapper<Contract> wrapper = new QueryWrapper<>();
		wrapper.in("id",contractIdList);
		List<Contract> contractList = contractService.list(wrapper);
		return this.getVoByContractList(contractList);
	}

	/**
	 * 根据Contract获取ContractVo
	 * @param contract 合同
	 * @return Vo对象
	 */
	@Override
	public ContractVo getVoByContract(Contract contract) {
		if (contract != null) {
			ContractVo contractVo = new ContractVo();
			// 将原始数据复制到contractVo中
			BeanUtils.copyProperties(contract,contractVo);

			// 构造ContractState对象
			ContractState contractState = contractStateService.getById(String.valueOf(contract.getStateId()));
			// 构造List<ContractField>对象
			List<ContractField> fields = contractFieldService.getFieldsOfContract(contract);
			// 构造Client对象
			Client client = clientService.getById(contract.getSecondPartyId());
			// 构造ContractType对象
			ContractType contractType = contractTypeService.getById(contract.getContractTypeId());

			// contractVo属性赋值
			contractVo.setFieldList(fields);
			contractVo.setContractState(contractState);
			contractVo.setClient(client);
			contractVo.setContractType(contractType);
			return contractVo;
		}
		return null;
	}


	/**
	 * 根据List of Contract 获取 List of ContractVo
	 * @param contractList List of Contract
	 * @return List of ContractVo
	 */
	@Override
	public List<ContractVo> getVoByContractList(List<Contract> contractList) {
		if (contractList != null) {
			ArrayList<ContractVo> contractVoList = new ArrayList<>(contractList.size());
			for (Contract contract : contractList) {
				ContractVo contractVo = this.getVoByContract(contract);
				contractVoList.add(contractVo);
			}
			return contractVoList;
		}
		return null;
	}

	/**
	 * 根据合同类型id查询所有合同vo
	 * @param typeId 合同类型Id
	 * @return 所有该类型的合同vo
	 */
	@Override
	public List<ContractVo> getVoByContractTypeId(Integer typeId) {
		List<Contract> contractList = contractService.getContractByTypeId(typeId);
		return this.getVoByContractList(contractList);
	}

	/**
	 * 根据合同类型id分页查询所有合同vo
	 * @param typeId  合同类型id
	 * @param current 当前页
	 * @param limit   页大小
	 * @return 分页查询结果
	 */
	@Override
	public Page<ContractVo> getVoByContractTypeId(Integer typeId, Long current, Long limit) {
		// 首先分页查询po数据
		Page<Contract> contractPage = contractService.getContractByTypeId(typeId, current, limit);
		List<Contract> contractList = contractPage.getRecords();

		// 根据contractList获取voList
		List<ContractVo> voList = this.getVoByContractList(contractList);

		// 复制contractPage至voPage中
		Page<ContractVo> voPage = new Page<>(current,limit);
		BeanUtils.copyProperties(contractPage,voPage);

		// 将voList保存到voPage中
		return voPage.setRecords(voList);
	}

	/**
	 * 根据合同状态id查询所有合同
	 * @param stateId 合同状态id
	 * @return 所有该状态的合同vo
	 */
	@Override
	public List<ContractVo> getVoByContractStateId(Integer stateId) {
		List<Contract> contractList = contractService.getContractByStateId(stateId);
		return this.getVoByContractList(contractList);
	}

	/**
	 * 根据合同状态id分页查询所有合同vo
	 * @param stateId 合同状态id
	 * @param current 当前页
	 * @param limit   页大小
	 * @return 分页查询结果
	 */
	@Override
	public Page<ContractVo> getVoByContractStateId(Integer stateId, Long current, Long limit) {
		// 首先分页查询contract
		Page<Contract> contractPage = contractService
				.getContractByStateId(stateId, current, limit);
		List<Contract> contractList = contractPage.getRecords();

		// 根据contractList获取voList
		List<ContractVo> voList = this.getVoByContractList(contractList);

		// 复制contractPage至voPage中
		Page<ContractVo> voPage = new Page<>(current, limit);
		BeanUtils.copyProperties(contractPage,voPage);

		// 保存voList至voPage
		return voPage.setRecords(voList);
	}

	/**
	 * 多条件查询合同
	 * @param conditions 条件集合
	 * @return 符合条件的模板
	 */
	@Override
	public List<ContractVo> MultiConditionSelect(Map<String, String> conditions) {
		QueryWrapper<Contract> wrapper = new QueryWrapper<>();
		for (String key : conditions.keySet()) {
			String value = conditions.getOrDefault(key, null);
			if ("contractState".equals(key)) { // 对于合同状态，需要将其转化为状态id
				Integer stateId = contractStateService.getStateIdByLikeName(value);
				WrapperUtils.eq("stateId",stateId,wrapper);
			} else {
				WrapperUtils.eq(key,value,wrapper);
			}
		}
		List<Contract> contractList = contractService.list(wrapper);
		return this.getVoByContractList(contractList);
	}
}
